package com.jy.api.domain;

import com.jy.api.exception.MyException;
import com.jy.api.util.UuidUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.Collection;
import java.util.Date;

/**
 * Created by hanxi on 2017/8/4.
 */
@Entity
@Data
@NoArgsConstructor
public class MemberAccount {
    @Id
    @Column(length = 50)
    private String id;
    private String storeId;

    private String name;

    /**
     * 推荐人id
     */
    private String referralUserId;
    /**
     * 推荐人名称
     */
    private String referralUserName;
    /**
     * 用户充值金额。会员消费时，默认先从该值扣减。如果余额不足，再从赠送金额扣减。不过赠送金额扣减时，要检查商品是否允许使用赠送金额
     */
    private double amount = 0;
    /**
     * 商户赠送金额
     */
    private double bonus = 0;
    private long points = 0;
    private boolean enabled = true;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
    @Column(insertable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    //会员上次支付时间，用于判定会员是否流失
    private Date lastConsumeTime;

    private boolean use;

    public MemberAccount(String storeId, String referralUserId, String referralUserName) {
        this.id = UuidUtil.getUuid();
        this.storeId = storeId;
        this.referralUserId = referralUserId;
        this.referralUserName = referralUserName;
    }

    public void pay(Order order, Collection<Product> products, MemberAccountTransaction memberAccountTransaction) throws MemberBonusPayNotAllowedException, MemberAccountNotEnoughException {
        if (amount >= order.getAmount()) {
            // 如果余额足够，则先扣减余额
            amount -= order.getAmount();
            memberAccountTransaction.setAmountBefore(memberAccountTransaction.getAmountAfter() - order.getAmount());
            memberAccountTransaction.setAmount(order.getAmount());
        } else {
            double surplusAmount = amount;
            double gap = order.getAmount() - surplusAmount;
            if (bonus >= gap) {
                amount = 0;
                bonus -= gap;
                memberAccountTransaction.setAmountBefore(memberAccountTransaction.getAmountAfter() - surplusAmount);
                memberAccountTransaction.setAmount(surplusAmount);

                memberAccountTransaction.setBonusBefore(memberAccountTransaction.getBonusAfter() - gap);
                memberAccountTransaction.setBonus(gap);
            } else {
                throw new MyException("支付失败！会员余额不足！");
            }
            amount = Math.rint(amount * 100) / 100d;
            bonus = Math.rint(bonus * 100) / 100d;
        }
    }


    public void pay(Order order, MemberAccountTransaction memberAccountTransaction) throws MemberBonusPayNotAllowedException, MemberAccountNotEnoughException {
        if (getTotalAmount() >= order.getAmount()) {
            double payamunt = order.getAmount();
            if (amount >= order.getAmount()) {
                // 如果余额足够，则先扣减余额
                amount -= payamunt;
                memberAccountTransaction.setAmountBefore(memberAccountTransaction.getAmountAfter() - order.getAmount());
                memberAccountTransaction.setAmount(order.getAmount());
            } else {
                payamunt -= amount;
                bonus -= payamunt;
                memberAccountTransaction.setAmountBefore(memberAccountTransaction.getAmountAfter() - amount);
                memberAccountTransaction.setAmount(amount);
                amount = 0;
                memberAccountTransaction.setBonusBefore(memberAccountTransaction.getBonusAfter() - payamunt);
                memberAccountTransaction.setBonus(payamunt);
                amount = Math.rint(amount * 100) / 100d;
                bonus = Math.rint(bonus * 100) / 100d;
            }
        } else {
            throw new MyException("支付失败！会员余额不足！");
        }


    }

    public double getTotalAmount() {
        return Math.rint((amount + bonus) * 100) / 100d;
    }

}
